package com.qyb.zx;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;

/**
 * <p>
 * 实现功能：
 * <p/>
 *
 * @author 秦有宝
 * @version 1.0.00
 */
public class ZxingDecoderHandler {
    /**
     * @param imgPath
     * @return String
     */
    public String decode(String imgPath) {
        BufferedImage image = null;
        Result result = null;
        try {
            image = ImageIO.read(new File(imgPath));
            if (image == null) {
                System.out.println("the decode image may be not exit.");
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

            Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
            Vector<BarcodeFormat> decodeFormats = new Vector<>();
            decodeFormats.add(BarcodeFormat.CODE_128);
            decodeFormats.add(BarcodeFormat.CODE_93);
            decodeFormats.add(BarcodeFormat.CODE_39);
            hints.put(DecodeHintType.POSSIBLE_FORMATS,decodeFormats);
            hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
            //优化精度
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            result = new MultiFormatReader().decode(bitmap, hints);
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String imgPath = "D:\\tt\\code\\TWOT.png";
        ZxingDecoderHandler handler = new ZxingDecoderHandler();
        String decodeContent = handler.decode(imgPath);
        System.out.println(decodeContent);
    }
    @Test
    public void pdfToImage1(){

        File file = new File("D:\\tt\\7.pdf");
        BufferedImage bufferedImage = null;
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                // 方式1,第二个参数是设置缩放比(即像素)
                //bufferedImage = renderer.renderImageWithDPI(i, 296);
                // 方式2,第二个参数是设置缩放比(即像素)
                bufferedImage = renderer.renderImage(i, 300 / 72f, ImageType.GRAY);
//                ImageIO.write(bufferedImage, "PNG", new File("D:\\tt\\111.png"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //
        // BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filepath));
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap bitmap = new BinaryBitmap(binarizer);
        HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>();
        //乱码。
        // 解码设置编码方式为：utf-8，
        decodeHints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        //优化精度
        decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
//复杂模式，开启PURE_BARCODE模式
        decodeHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        Vector<BarcodeFormat> decodeFormats = new Vector<>();
        decodeFormats.add(BarcodeFormat.CODE_128);
        decodeHints.put(DecodeHintType.POSSIBLE_FORMATS,decodeFormats);
        //开启PURE_BARCODE模式。（这是解决我的方案，带图片LOGO的解码方案）
        //复杂模式，开启PURE_BARCODE模式
        //decodeHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        Result result = null;
        try {
            result = new MultiFormatReader().decode(bitmap, decodeHints);
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        String s =  result.getText();
        System.out.println(s);
    }
}
